<template>
  <div class="wrapper">
    <!-- 顶部 -->
    <div class="top">在线商城系统</div>

    <!-- 主体 -->
    <div class="main">
      <!-- 左侧 -->
      <div class="left">
        <img src="../assets/register.png" style="width: 100%;">
      </div>
      <!-- 右侧 -->
      <div class="right">
        <!-- 标题 -->
        <div class="title">欢迎注册</div>
        <!-- 表单 -->
        <el-form :model="user" :rules="rules" ref="userForm" size="normal">
          <el-form-item prop="userName">
            <el-input placeholder="请输入账号" prefix-icon="el-icon-user" v-model="user.userName"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" show-password v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="confirmPassword">
            <el-input placeholder="请输入确认密码" prefix-icon="el-icon-lock" show-password v-model="user.confirmPassword"></el-input>
          </el-form-item>
          <el-form-item prop="nickname">
            <el-input placeholder="请输入昵称" prefix-icon="el-icon-user-solid" v-model="user.nickname"></el-input>
          </el-form-item>
          <el-form-item prop="email">
            <el-input placeholder="请输入邮箱" prefix-icon="el-icon-message" v-model="user.email"></el-input>
          </el-form-item>
          <el-form-item prop="phone">
            <el-input placeholder="请输入电话" prefix-icon="el-icon-phone" v-model="user.phone"></el-input>
          </el-form-item>
          <el-form-item prop="address">
            <el-input placeholder="请输入地址" prefix-icon="el-icon-s-home" v-model="user.address"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="register">注&nbsp;&nbsp;&nbsp;&nbsp;册</el-button>
            <el-button type="warning" @click="$router.push('/login')">返回登录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: {},
      rules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          {pattern: /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/, message: '邮箱格式不正确', trigger: 'blur'}
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' },
          { pattern: /^1[3456789]\d{9}$/, message: '电话格式不正确', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ],
      }
    }
  },
  methods: {
    register() {
      this.$refs['userForm'].validate((valid) => {
        if (!valid) return 
         
        // 判断两次输入的密码是否一致
        if (this.user.password !== this.user.confirmPassword) {
          return this.$message.error("两次输入的密码不一致")
        }
        // 发送请求
        this.request.post("/user/register", this.user).then(res => {
          if(res.code === 200) {
            this.$message.success("注册成功")
            this.$router.push("/login")
          } else {
            this.$message.error(res.msg)
          }
        })
      });
    }
  }
}
</script>


<style scoped style="less">
.wrapper {
  height: 100vh;
  background-image: linear-gradient(to bottom right, #4169E1, #87CEFA);
  overflow: hidden;
  /* 顶部 */
  .top {
    height: 60px;
    line-height: 60px;
    font-size: 20px;
    padding-left: 50px;
    color: white;
    background-color: rgba(0, 0, 0, 0.2)
  }
  /* 主体 */
  .main {
    display: flex;
    width: 50%;
    margin: 150px auto;
    background-color: white;
    border-radius: 10px;
    overflow: hidden;
    /* 左侧 */
    .left{
      width: 40%;
    }
    /* 右侧 */
    .right {
      width: 350px;
      margin-top: 30px;
      margin-left: 50px;

      .title {
        margin: 20px 0;
        text-align: center;
        font-size: 24px;
        font-weight: bold;
        letter-spacing: 6px;
      }
    }
  }
}
</style>